package com.easyjava.builder.strategy.impl;

import com.easyjava.bean.Constants;
import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.builder.BuildMapperXml;
import com.easyjava.builder.strategy.inf.MapperXmlStrategy;
import java.io.BufferedWriter;
import java.io.IOException;

/**
 * 基础查询条件
 */
public class XmlFieldBaseQueryConditionImpl implements MapperXmlStrategy {

    @Override
    public void generateMapperXml(TableInfo tableInfo, BufferedWriter bw) throws IOException {
        bw.write("\t<!--基础查询条件-->\n");
        bw.write("\t<sql id=\"" + BuildMapperXml.BASE_QUERY_CONDITION + "\">\n");
        for (FieldInfo field : tableInfo.getFieldList()) {
            String propertyName = BuildMapperXml.SELECT_PREFIX + field.getPropertyName();
            String str = "";
            if (Constants.TYPE_STRING.equals(field.getJavaType()) || Constants.TYPE_DATE.equals(field.getJavaType())) {
                str = " and " + propertyName + " != ''";
            }
            bw.write("\t\t<if test=\"" + propertyName + " != null" + str + "\">\n");
            bw.write("\t\t\tand " + field.getFieldName() + " = #{" + propertyName + "}\n");
            bw.write("\t\t</if>\n");
        }
        bw.write("\t</sql>\n\n");
    }
}
